package com.sevenstudio.adu.gasing.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;

import android.view.KeyEvent;

import com.sevenstudio.adu.gasing.bluetooth.BT_Server_Client;
import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.define.iServer_Data;
import com.sevenstudio.adu.gasing.element.ABackground;
import com.sevenstudio.adu.gasing.element.AScene;
import com.sevenstudio.adu.gasing.element.ASound;
import com.sevenstudio.adu.gasing.element.ASprite;
import com.sevenstudio.adu.gasing.element.AText;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Menu_Scroll_Y;
import com.sevenstudio.adu.gasing.engine.Utils;

public class State_Select_Arena 
{
	GameEngine engine;
	AScene scene;
	ASprite bg_pembatas;
	ABackground bg_Arena;
	
	Menu_Scroll_Y scroll_Y;
	
		
	ASound sfx_slide;
	
	ASprite[] spr_arena = new ASprite[3];
	private AText aText;
	
	public State_Select_Arena(GameEngine engine)
	{
		this.engine = engine;
		scene = new AScene(0);
		load();
		
		scene.theScene().setOnAreaTouchListener(engine);
		scene.theScene().setOnSceneTouchListener(engine);
		
		for(int i=0; i < spr_arena.length; i++)
		{
			scene.theScene().registerTouchArea(spr_arena[i].theSprite());
		}
	}

	private void load() 
	{
		sfx_slide = new ASound(engine, iGameDefines.SFX_SLIDE);
		
		bg_pembatas = new ASprite(engine, iGameDefines.SPRITE_ARENA, 0);
		bg_Arena = new ABackground(engine, iGameDefines.SPRITE_ARENA, 1);
		
		aText = new AText(engine, iGameDefines.TEXT_ARENA, 0, engine.gameFont[2]);
				
		for(int i =0; i < 3; i++)
		{
			spr_arena[i] = new ASprite(engine, iGameDefines.SPRITE_ARENA, i+2);
		}
		
		scroll_Y = new Menu_Scroll_Y(engine, sfx_slide.theSound(), 0, spr_arena[0].theSprite(), spr_arena[1].theSprite(), spr_arena[2].theSprite());
		
	}

	public void Init() 
	{
		engine.SetGameScene(scene);
		
		engine.SceneAttach(scene, bg_Arena);
		for(int i=0; i < 3; i++)
		{
			engine.SceneAttach(scene, spr_arena[i]);
		}
		
		engine.SceneAttach(scene, aText);
		engine.SceneAttach(scene, bg_pembatas);
		
		
	}

	public void Update() 
	{
		if (BT_Server_Client.isClientWaiting && BT_Server_Client.isServerWaiting)
		{
			engine.SetNextMessageState(0);
			engine.SetNextGameState(iGameDefines.GAME_STATE_GAMEPLAY);
		}

		for(int i=0; i < spr_arena.length; i++)
		{
			if(scroll_Y.getFocus() == i)
			{
				aText.theText().setText(iGameDefines.ARENA[i]);
			}
		}
		
	}

	public void Exit() 
	{
		final EngineLock lock = engine.getEngine().getEngineLock();
		lock.lock();
		
		bg_pembatas.DetachSelf();
		bg_Arena.DetachSelf();
		
		for(int i=0; i < 3; i++)
		{
			spr_arena[i].DetachSelf();
		}
		aText.theText().detachSelf();
		
		lock.unlock();
	}

	public void SceneTaouch(Scene arg0, TouchEvent arg1)
	{
		scroll_Y.onTouchEvent(arg1);

		
	}

	public void keyDown(int keyCode, KeyEvent event) 
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			if(engine.messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
			{
				Exit();
				engine.SetNextMessageState(0);
			}
			if(BT_Server_Client.isBluetooth)
			{
				engine.SetNextGameState(iGameDefines.GAME_STATE_SELECT_PLAYER);
			}
			else
			{
				engine.SetNextGameState(iGameDefines.GAME_STATE_SELECT_RIVAL);
			}
		}
		
	}

	public void Touch(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {

		for(int i=0; i < spr_arena.length; i++)
		{
			if(pSceneTouchEvent.isActionUp() && pTouchArea.equals(spr_arena[i].theSprite()))
			{
//				scene.theScene().detachChildren();
//				
//				iServer_Data.INDEX_ARENA = i;
//				engine.SetNextMessageState(0);
//				engine.SetNextGameState(iGameDefines.GAME_STATE_GAMEPLAY);
			}
		}
	}
	
	public void onClick(ClickDetector pClickDetector, int pPointerID,
			float pSceneX, float pSceneY)
	{
		for(int i=0; i < spr_arena.length; i++)
		{
			if (Utils.isOnArea(pSceneX, pSceneY, spr_arena[i].theSprite())
					&& scroll_Y.getFocus() == i)
			{
	//			Utils.TRACE("Click"+i);
				scene.theScene().detachChildren();
				State_Main_Menu.bgm_menu.Stop();
				iServer_Data.INDEX_ARENA = i;
				
				if(BT_Server_Client.isBluetooth)
				{
					BT_Server_Client.isServerWaiting = true;
					engine.server_Client.sendMessage("map," +i);	
					engine.server_Client.sendMessage("play,0");	
				}
				else
				{
					engine.SetNextMessageState(0);
					engine.SetNextGameState(iGameDefines.GAME_STATE_GAMEPLAY);
				}
			}
		}
	}
	
}